//
//  ContractList.m
//  Opper
//
//  Created by Le Dung on 4/14/13.
//  Copyright (c) 2013 LeDung. All rights reserved.
//

#import "ContractList.h"

@interface ContractList ()

@end

@implementation ContractList
@synthesize buttonContainView,contractTable;
@synthesize contractCount,contractArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewDidAppear:(BOOL)animated
{
    [StyleClass barButtonItemEdit:self left:YES right:NO];
}

-(void)leftPress
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [StyleClass createNavi:self andTitle:@"Abonnementem"];
    [StyleClass background:self.view];
    [self createButton];
    [self locationChange];
    [self loadData];
    [self createTable];
}


-(void)loadData
{
    self.contractArray =[[NSMutableArray alloc]init];
    self.contractArray =[GetData getContractList:self.member.idMember];
    self.userName.text=[[self.member.firstName stringByAppendingString:@" "] stringByAppendingString:self.member.lastname];
    
}
-(void)createTable
{

    
    contractTable=[[UITableView alloc  ]initWithFrame:CGRectMake(10, 40, 300,200) style:UITableViewStylePlain];
    
   
    [self.view addSubview:contractTable];
    contractTable.delegate= self;
    contractTable.dataSource=self;
    contractTable.layer.borderColor=[UIColor blackColor].CGColor;
    contractTable.layer.borderWidth=1.0;
}

-(void)locationChange
{
    CGRect frame =self.buttonContainView.frame;
    frame.origin.y =frame.origin.y+10+ self.contractCount*30;
    self.buttonContainView.frame=frame;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.contractArray count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"ContractListCell";
    ContractListCell *cell = (ContractListCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObj =  [[NSBundle mainBundle] loadNibNamed:@"ContractListCell" owner:self options:nil];
        for (id currentObj in topLevelObj)
        {
            if ([currentObj isKindOfClass:[UITableViewCell class]])
            {
                cell = (ContractListCell*)currentObj;
                break;
            }
        }
        
    ContractInfo *contract=[self.contractArray objectAtIndex:indexPath.row];
    cell.contractTitle.text =contract.contractName;
        cell.detailButton.tag=indexPath.row;
    [cell.detailButton addTarget:self action:@selector(detailPress:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return cell;
}


-(IBAction)detailPress:(UIButton*)button 
{
    ContractDetail *page =[[[ContractDetail alloc]init]autorelease];
    page.contractUnit =self.contractArray[button.tag];
    page.member=self.member;
    [StyleClass  currentScreen:self andDestination:page andHideBackButton:YES];
    
    
}
-(void)createButton
{

    buttonContainView=[[UIView alloc]initWithFrame:CGRectMake(10, 250, 200, 60)];
    [self.view addSubview:self.buttonContainView];
    [buttonContainView release];

    
    UIButton *addContract=[UIButton buttonWithType:UIButtonTypeCustom];
    addContract.frame =CGRectMake(0 , 0, 50, 50);
    [addContract setBackgroundImage:[UIImage imageNamed:@"AddOper.png"] forState:UIControlStateNormal];
    [addContract addTarget:self action:@selector(addPress) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonContainView addSubview:addContract];

    
    UIButton *movSev=[UIButton buttonWithType:UIButtonTypeCustom];
    movSev.frame =CGRectMake(60 , 0, 50, 50);
    [movSev setBackgroundImage:[UIImage imageNamed:@"GreenSer.png"] forState:UIControlStateNormal];
    [movSev addTarget:self action:@selector(movPress) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonContainView addSubview:movSev];
    
    UIButton *ripSev=[UIButton buttonWithType:UIButtonTypeCustom];
    ripSev.frame =CGRectMake(120 , 0, 50, 50);
    [ripSev setBackgroundImage:[UIImage imageNamed:@"GrayAdd.png"] forState:UIControlStateNormal];
    [ripSev addTarget:self action:@selector(ripPress) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonContainView addSubview:ripSev];
}

-(void)addPress
{
    ContractAddNew *page=[[[ContractAddNew alloc]init]autorelease];
    page.member =self.member;
    [StyleClass currentScreen:self andDestination:page andHideBackButton:YES];
}

-(void)movPress
{
    MovService *page=[[[MovService alloc]init] autorelease];
    page.member =self.member;
    [StyleClass currentScreen:self andDestination:page andHideBackButton:YES];
}

-(void)ripPress
{
    DeathService *page=[[[DeathService alloc]init] autorelease];
    page.member=self.member;
    [StyleClass currentScreen:self andDestination:page andHideBackButton:YES];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {

    [contractTable release];
    [buttonContainView release];
    [_userName release];
    [super dealloc];
}
@end
